package client.comm.baoding.fragment

import android.view.View
import client.comm.commlib.base.BaseFragment
import client.comm.commlib.comm_ui.draw.SpaceItemDecoration
import client.comm.commlib.widget.EmptyLayout
import client.comm.baoding.R
import client.comm.baoding.adapter.OrderAdapter
import client.comm.baoding.api.bean.GoodsOrder
import client.comm.baoding.api.bean.SuccessEvent
import client.comm.baoding.databinding.FragmentLoadmorepageBinding
import client.comm.baoding.dialog.MsgTitleDialog
import client.comm.baoding.ui.vm.MeOrderViewModel
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class GoodsOrderOneFragment : BaseFragment<FragmentLoadmorepageBinding, MeOrderViewModel>() {
    override fun getViewId(): Int {
        return R.layout.fragment_loadmorepage
    }

    val adapter: OrderAdapter by lazy {
        OrderAdapter(requireContext(), mImgLoader).apply {
            orderPay = {
                viewModel.orderPay(it.order_id)
            }

            cancle = {
                MsgTitleDialog(requireContext()).apply {
                    okTxt = "确定"
                    cancleTxt = "不"
                    title = "确定取消订单？"
                    ok = {
                        viewModel.cancle(it.order_id)
                    }
                }.show()
            }

            qrsh = {
                MsgTitleDialog(requireContext()).apply {
                    title = "是否确认收货？"
                    ok = {
                        viewModel.qrshCall(it.order_id)

                    }
                    okTxt = "确认"
                    cancleTxt = "取消"
                }.show()
            }
        }
    }

    override fun initViewModel(): MeOrderViewModel = createViewModel()

    override fun initWidget() {

        binding.emptyLayout.setOnClickListener {
            viewModel.pageOne.value = 0
            viewModel.holdOne()
        }
        binding.recyclerView.addItemDecoration(SpaceItemDecoration(10))

        binding.recyclerView.adapter = adapter
        binding.recyclerView.onLoadMore = {
            if (adapter.hasMoreState.value!! && !viewModel.isOneLoading.value!!) {
                viewModel.holdOne()
            }
        }
        binding.refreshLayout.setOnRefreshListener {
            adapter.hasMoreState.value = true

            viewModel.pageOne.value = 0
            viewModel.holdOne()
        }
        viewModel.isOneLoading.observe(this, {
            if (!it) {
                binding.refreshLayout.isRefreshing = false
            }
        })
        viewModel.callOne.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    val list =
                        it.data?.order_list as MutableList<GoodsOrder.Order?>?
                    if (viewModel.pageOne.value == 1) {
                        adapter.submitList(list)
                    } else
                        adapter.appendList(list)
                    if (list != null && list.size > 0) {
                        if (list.size < 10) {
                            adapter.hasMoreState.value = false
                        }
                        binding.emptyLayout.visibility = View.GONE
                    } else {
                        if (adapter.itemCount == 0)
                            binding.emptyLayout.errorType = EmptyLayout.NODATA
                        adapter.hasMoreState.value = false
                    }
                } else {
                    if (adapter.itemCount == 0)
                        binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })
    }

    override fun onResume() {
        super.onResume()
        adapter.hasMoreState.value = true

        viewModel.pageOne.value = 0
        viewModel.holdOne()
    }


}